package com.fand.manager.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fand.common.exception.BusinessException;
import com.fand.common.util.PageResult;
import com.fand.common.util.PageUtil;
import com.fand.common.util.QueryWrapperUtil;
import com.fand.manager.entity.SysRole;
import com.fand.manager.mapper.SysRoleMapper;
import com.fand.manager.model.role.SysRolePostDTO;
import com.fand.manager.model.role.SysRoleReturnDTO;
import com.fand.manager.model.role.SysRoleSearchDTO;
import com.fand.manager.service.ISysRoleService;
import com.fand.manager.service.ISysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author javaboy
 * @since 2021-06-07
 */
@Service
public class SysRoleServiceImpl implements ISysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private ISysUserService sysUserService;

    @Transactional
    @Override
    public Boolean saveRole(SysRolePostDTO sysRolePostDTO) {
        if (Objects.isNull(sysRolePostDTO) || Objects.isNull(sysRolePostDTO.getName()) || Objects.isNull(sysRolePostDTO.getCode())) {
            throw new BusinessException("数据不完整");
        }
        SysRole sysRole = SysRole.builder().build();
        BeanUtil.copyProperties(sysRolePostDTO, sysRole);
        int rows = sysRoleMapper.insert(sysRole);
        if (rows < 1) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    @Override
    public PageResult<List<SysRoleReturnDTO>> listRoles(SysRoleSearchDTO sysRoleSearchDTO) {
        QueryWrapper queryWrapper = QueryWrapperUtil.builder().queryWrapper(new QueryWrapper()).object(sysRoleSearchDTO).build().builderQueryParams();
        Page page = PageUtil.page(sysRoleSearchDTO);
        PageResult<List<SysRoleReturnDTO>> listPageResult = PageUtil.resultToClass(sysRoleMapper.selectPage(page, queryWrapper), SysRoleReturnDTO.class);

        listPageResult.setData(listPageResult.getData().stream().map(sysRoleReturnDTO -> {
            sysRoleReturnDTO.setCreatedByName(sysUserService.findUser(sysRoleReturnDTO.getCreatedBy()).getUserName());
            sysRoleReturnDTO.setUpdatedByName(sysUserService.findUser(sysRoleReturnDTO.getUpdatedBy()).getUserName());
            return sysRoleReturnDTO;
        }).collect(Collectors.toList()));
        return listPageResult;
    }

    @Transactional
    @Override
    public Boolean deleteByIds(List<Long> RoleIds) {
        if(CollUtil.isEmpty(RoleIds)){
            throw new BusinessException("请选中要删除的角色");
        }
        int rows = sysRoleMapper.deleteBatchIds(RoleIds);
        if(rows <1){
            throw new BusinessException("删除角色失败");
        }
        return Boolean.TRUE;
    }

    @Override
    public SysRoleReturnDTO findRole(Long id) {
        if(Objects.isNull(id) || id <1){
            throw new BusinessException("请选中要修改的角色");
        }
        SysRole sysRole = sysRoleMapper.selectById(id);
        if(Objects.nonNull(sysRole)){
            SysRoleReturnDTO sysRoleReturnDTO = SysRoleReturnDTO.builder().build();
            BeanUtil.copyProperties(sysRole,sysRoleReturnDTO);
            return sysRoleReturnDTO;
        }
        return SysRoleReturnDTO.builder().build();
    }

    @Transactional
    @Override
    public Boolean updateRole(SysRolePostDTO sysRolePostDTO) {
        if(Objects.isNull(sysRolePostDTO) ||Objects.isNull(sysRolePostDTO.getId()) || sysRolePostDTO.getId() <1){
            throw new BusinessException("数据不完整");
        }
        SysRole sysRole = SysRole.builder().build();
        BeanUtil.copyProperties(sysRolePostDTO,sysRole);
        int row = sysRoleMapper.updateById(sysRole);
        if(row < 0){
            throw new BusinessException("修改数据失败");
        }
        return Boolean.TRUE;
    }

}
